﻿using System.Linq;

namespace Nxg.Blog.Core.ViewProjections.Admin {
    public class AllStatisticsViewModel {
        public long PostsCount { get; set; }

        public long CommentsCount { get; set; }

        public int TagsCount { get; set; }
    }

    public class AllStatisticsBindingModel {
        public AllStatisticsBindingModel () {
            TagThreshold = 1;
        }

        public int TagThreshold { get; set; }
    }

    public class AllStatisticsViewProjection : IViewProjection<AllStatisticsBindingModel, AllStatisticsViewModel> {
        private readonly DataContext _db;

        public AllStatisticsViewProjection (DataContext db) {
            _db = db;
        }

        public AllStatisticsViewModel Project (AllStatisticsBindingModel input) {
            var postCount = _db.Post.Count (x => true);
            if (postCount == 0)
                return new AllStatisticsViewModel ();

            var stat = new AllStatisticsViewModel {
                PostsCount = postCount,
                CommentsCount = _db.Comment.Count (x => true)
            };

            stat.TagsCount = (int) _db.Tag.Count (x => true);
            return stat;
        }
    }
}